package org.starsource.forum.server.dao.test;

import static org.junit.Assert.*;
import java.util.Random;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import org.starsource.forum.client.exception.DataNotFoundException;
import org.starsource.forum.client.exception.DatabaseException;
import org.starsource.forum.client.exception.UserNotFoundException;
import org.starsource.forum.server.dao.UserDao;
import org.starsource.forum.server.domain.User;

public abstract class AbsTestUserDao {
	private static org.apache.log4j.Logger logger = org.apache.log4j.LogManager
	.getLogger(AbsTestUserDao.class.getName());

	@Autowired
	private UserDao userDao;
	
	@Test
	public void testSave() throws DatabaseException {
		User user1 = new User("test", "test", "测试用户", "DIRECTORY_FOR_TEST");
		userDao.save(user1);
		
		User user2 = userDao.getUser(user1.getId());
		logger.debug("User Name:" + user2.getName());
		assertEquals(user1, user2);

		User user3 = new User("", "", null, null);
		userDao.save(user3);
		
		User user4 = userDao.getUser(user3.getId());
		assertEquals(user3, user4);
	}

	@Test
	public void testUpdate() throws DatabaseException {
		User user1 = new User("ONLY_FOR_UNIT_TEST", "PASSWORD_FOR_TEST", "张三John", "DIRECTORY_FOR_TEST");
		userDao.save(user1);
		
		int userId = user1.getId();
		
		user1.setAccount("new_account_for_test");
		user1.setPassword("new_password_for_test");
		user1.setName("new_name_for_test");
		user1.setDirectory("new_directory_for_test");
		userDao.update(user1);
		
		User user2 = userDao.getUser(userId);
		assertEquals(user1, user2);

//		user1.setAccount(null);
//		user1.setPassword(null);
		user1.setName(null);
		user1.setDirectory(null);
		userDao.update(user1);
		
		User user3 = userDao.getUser(userId);
		assertEquals(user1, user3);
	}

	@Test
	public void testDelete() throws UserNotFoundException, DatabaseException {
		User user1 = new User("ONLY_FOR_UNIT_TEST", "PASSWORD_FOR_TEST", "张三John", "DIRECTORY_FOR_TEST");
		userDao.save(user1);
		
		int userId = user1.getId();
		
		userDao.delete(user1);
		
		try {
			userDao.getUser(userId);
			assertTrue(false);
		} catch (DataNotFoundException e) {
		}
	}

	@Test
	public void testGetUserById() throws UserNotFoundException, DatabaseException {
		User user1 = new User("ONLY_FOR_UNIT_TEST", "PASSWORD_FOR_TEST", "张三John", "DIRECTORY_FOR_TEST");
		userDao.save(user1);
		
		User user2 = userDao.getUser(user1.getId());
		assertEquals(user1, user2);
	}
	
	@Test
	public void testGetUserByAccount() throws DatabaseException {
		Random random = new Random();
		User user1 = new User("ACC_" + random.nextInt(), "PASSWORD_FOR_TEST", "张三John", "DIRECTORY_FOR_TEST");
		userDao.save(user1);
		
		User user2 = userDao.getUser(user1.getAccount());
		assertEquals(user1, user2);
	}
	
	@Test
	public void testGetMatchUser() throws DatabaseException {
		Random random = new Random();
		User user1 = new User("ACC_" + random.nextInt(), "PWD_" + random.nextInt(), "张三John", "DIRECTORY_FOR_TEST");
		userDao.save(user1);
		
		User user2 = userDao.getUser(user1.getAccount(), user1.getPassword());
		assertEquals(user1, user2);
	}
	
}
